package com.uchiha.nova.mainService.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.uchiha.nova.core.model.dto.ChinaOrgCodeDTO;
import cn.hutool.core.bean.BeanUtil;
import com.uchiha.nova.core.tool.constant.NovaCRUDMethodConstant;
import com.uchiha.nova.core.model.entity.ChinaOrgCodeEntity;
import com.uchiha.nova.core.model.vo.ChinaOrgCodeVO;
import com.uchiha.nova.core.model.query.ChinaOrgCodeQuery;
import com.uchiha.nova.core.tool.api.R;
import com.uchiha.nova.core.tool.support.BaseController;
import com.uchiha.nova.core.tool.support.Condition;
import com.uchiha.nova.core.tool.support.NovaUser;
import com.uchiha.nova.core.tool.support.QueryContext;
import com.uchiha.nova.core.tool.util.Func;
import com.uchiha.nova.mainService.service.IChinaOrgCodeService;
import com.uchiha.nova.mainService.wrapper.ChinaOrgCodeWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 中国区域机构代码 控制器
 *
 * @author Uchihashiamu
 */
@RestController
@AllArgsConstructor
@RequestMapping("/chinaOrgCode")
@Tag(name = "中国区域机构代码")
@Slf4j
public class ChinaOrgCodeController extends BaseController {

  @Resource
  IChinaOrgCodeService chinaOrgCodeService;
  	/**
	 * 详情
	 */
	@PostMapping(NovaCRUDMethodConstant.NOVA_WEB_GETONE)
	@Operation(summary = "详情")
	public R<ChinaOrgCodeVO> getOne(@RequestBody ChinaOrgCodeDTO chinaOrgCodeDTO) {
        ChinaOrgCodeEntity entity = new ChinaOrgCodeEntity();
        BeanUtil.copyProperties(chinaOrgCodeDTO, entity);
		ChinaOrgCodeEntity detail = chinaOrgCodeService.getOne(Condition.getQueryWrapper(entity));
		return R.data(ChinaOrgCodeWrapper.build().entityVO(detail));
	}

	/**
	 * 分页 代码自定义代号
	 */
	@GetMapping(NovaCRUDMethodConstant.NOVA_WEB_PAGE)
  @Operation(summary = "分页查询")
	public R getPage(ChinaOrgCodeQuery query, QueryContext queryContext) {
		IPage<ChinaOrgCodeEntity> pages = chinaOrgCodeService.page(Condition.getPage(queryContext), Condition.getQueryWrapper(query, queryContext,ChinaOrgCodeEntity.class));
		return R.data(ChinaOrgCodeWrapper.build().pageVO(pages));
	}

	/**
	 * 新增 代码自定义代号
	 */
	@PostMapping(NovaCRUDMethodConstant.NOVA_WEB_SAVE)
	@Operation(summary = "新增")
	public R save(@RequestBody ChinaOrgCodeDTO chinaOrgCodeDTO) {
        ChinaOrgCodeEntity entity = new ChinaOrgCodeEntity();
        BeanUtil.copyProperties(chinaOrgCodeDTO, entity);
		return R.status(chinaOrgCodeService.save(entity));
	}

	/**
	 * 修改 代码自定义代号
	 */
	@PostMapping(NovaCRUDMethodConstant.NOVA_WEB_UPDATE)
	@Operation(summary = "修改")
	public R update(@RequestBody ChinaOrgCodeDTO chinaOrgCodeDTO) {
        ChinaOrgCodeEntity entity = new ChinaOrgCodeEntity();
        BeanUtil.copyProperties(chinaOrgCodeDTO, entity);
		return R.status(chinaOrgCodeService.updateById(entity));
	}

	/**
	 * 新增或修改 代码自定义代号
	 */
	@PostMapping(NovaCRUDMethodConstant.NOVA_WEB_SAVEORUPDATE)
	@Operation(summary = "新增或修改")
	public R saveOrUpdate(@RequestBody ChinaOrgCodeDTO chinaOrgCodeDTO) {
        ChinaOrgCodeEntity entity = new ChinaOrgCodeEntity();
        BeanUtil.copyProperties(chinaOrgCodeDTO, entity);
		return R.status(chinaOrgCodeService.saveOrUpdate(entity));
	}

	@GetMapping("/tree")
	public R tree(@RequestParam(defaultValue = "370600000000") Long id) {
		List<ChinaOrgCodeVO> list = chinaOrgCodeService.tree(id);
		return R.data(list);
	}

	@GetMapping("/tree/station")
	public R treeStation(@RequestParam(defaultValue = "370600000000") Long id) {
		List<ChinaOrgCodeVO> list = chinaOrgCodeService.treeStation(id);
		return R.data(list);
	}

	@GetMapping("/tree/role")
	public R treeRole(@RequestParam(defaultValue = "370000000000") Long id) {
		List<ChinaOrgCodeVO> list = chinaOrgCodeService.treeRole(id);
		return R.data(list);
	}

	@GetMapping("/tree/data")
	public R treeData() {
		NovaUser novaUser = getNovaUser();
		String dataAuth = novaUser.getDataAuth();
		List<ChinaOrgCodeVO> list = chinaOrgCodeService.treeData(dataAuth);
		return R.data(list);
	}
}
